﻿#include "pmsrockpaperscissors.h"
#include "ui_pmsrockpaperscissors.h"

PMSRockPaperScissors::PMSRockPaperScissors(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PMSRockPaperScissors)
{
    ui->setupUi(this);

    setWindowTitle("PMS RPS 1.0");

    currGesture = 0;    //石头


    connect(ui->btn_allow, SIGNAL(clicked(bool)), this, SLOT(btn_allow_slot()));
    connect(ui->btn_rps, SIGNAL(clicked(bool)), this, SLOT(btn_rps_slot()));
    connect(ui->btn_tips, SIGNAL(clicked(bool)), this, SLOT(btn_tips_slot()));
}

void PMSRockPaperScissors::btn_tips_slot() {
    QMessageBox::about(this, QString("I know!"),
                       QStringLiteral("手势关系: \n"
                                      "[石头]=[石头] [石头]<[布] [石头]>[剪刀] [石头]<[石头-布] [石头]>[石头-剪刀] [石头]<[布-剪刀]\n"
                                      "[布]>[石头] [布]=[布] [布]<[剪刀] [布]>[石头-布] [布]<[石头-剪刀] [布]<[布-剪刀]\n"
                                      "[剪刀]<[石头] [剪刀]>[布] [剪刀]=[剪刀] [剪刀]<[石头-布] [剪刀]<[石头-剪刀] [剪刀]>[布-剪刀]\n"
                                      "[石头-布]>[石头] [石头-布]<[布] [石头-布]>[剪刀] [石头-布]?[石头-布] [石头-布]<[石头-剪刀] [石头-布]>[布-剪刀]\n"
                                      "[石头-剪刀]<[石头] [石头-剪刀]>[布] [石头-剪刀]>[剪刀] [石头-剪刀]>[石头-布] [石头-剪刀]?[石头-剪刀] [石头-剪刀]<[布-剪刀]\n"
                                      "[布-剪刀]>[石头] [布-剪刀]>[布] [布-剪刀]<[剪刀] [布-剪刀]<[石头-布] [布-剪刀]>[石头-剪刀] [布-剪刀]?[布-剪刀]\n"));
}

void PMSRockPaperScissors::btn_allow_slot() {



    int state[6][6] = {
        0, -1, 1, -1, 1, -1,
        1, 0, -1, 1, -1, -1,
        -1, 1, 0, -1, -1, 1,
        1, -1, 1, 5, -1, 1,
        -1, 1, 1, 1, 5, -1,
        1, 1, -1, -1, 1, 5
    };


    std::unordered_map<QString, int> map;

    map[QStringLiteral("石头")] = 0;
    map[QStringLiteral("布")] = 1;
    map[QStringLiteral("剪刀")] = 2;
    map[QStringLiteral("石头-布")] = 3;
    map[QStringLiteral("石头-剪刀")] = 4;
    map[QStringLiteral("布-剪刀")] = 5;

    int idx = QRandomGenerator::global()->bounded(6);

    qDebug() << idx;

    QString gesture[6] = {
        QStringLiteral("石头"),
        QStringLiteral("布"),
        QStringLiteral("剪刀"),
        QStringLiteral("石头-布"),
        QStringLiteral("石头-剪刀"),
        QStringLiteral("布-剪刀")
    };




    int res = state[map[ui->btn_rps->text()]][idx];

    ui->computer_idea->setText(gesture[idx]);
    ui->player_idea->setText(ui->btn_rps->text());


    qDebug() << res;

    if(!res) ui->label_res->setText(QStringLiteral("平局"));
    else if(res == 1) ui->label_res->setText(QStringLiteral("你赢了！"));
    else if(res == -1) ui->label_res->setText(QStringLiteral("你输了！"));
    else {

        int winner = QRandomGenerator::global()->bounded(2);

        if(!winner) ui->label_res->setText(QStringLiteral("你输了！"));
        else ui->label_res->setText(QStringLiteral("你赢了！"));


    }




}

void PMSRockPaperScissors::btn_rps_slot() {
    QString gesture[6] = {
        QStringLiteral("石头"),
        QStringLiteral("布"),
        QStringLiteral("剪刀"),
        QStringLiteral("石头-布"),
        QStringLiteral("石头-剪刀"),
        QStringLiteral("布-剪刀")
    };


    currGesture = (currGesture + 1) % 6;

    qDebug() << currGesture;

    ui->btn_rps->setText(gesture[currGesture]);

}


PMSRockPaperScissors::~PMSRockPaperScissors()
{
    delete ui;
}
